// 定义请求方法的类型
export type RequestMethod = 'GET' | 'POST' | 'PUT' | 'DELETE';
export type RequestDataType = 'json' | 'base64' | 'text' | 'arraybuffer';


// 定义请求配置的接口
export interface RequestOptions {
    url: string;
    method?: RequestMethod;
    headers?: { [key: string]: string };
    data?: any;
    dataType?: RequestDataType;
}

export class RequestService {
    private baseUrl: string;

    constructor(baseUrl = '') {
        this.baseUrl = baseUrl;
    }

    private request(options: RequestOptions): Promise<any> {
        return new Promise((resolve, reject) => {
            my.request({
                url: this.baseUrl + options.url,
                method: options.method || 'GET',
                headers: options.headers || {
                    'Content-Type': 'application/json'
                },
                data: options.data,
                dataType: options.dataType || "json",
                success: (res) => {
                    resolve(res.data);
                },
                fail: (err) => {
                    reject(err);
                },
                complete: () => {
                  //TODO:
                }
            });
        });
    }

    public get(url: string, params: any = {}, headers: { [key: string]: string } = {}): Promise<any> {
        return this.request({
            url,
            method: 'GET',
            headers,
            data: params
        });
    }

    public post(url: string, data: any = {}, headers: { [key: string]: string } = {}): Promise<any> {
        return this.request({
            url,
            method: 'POST',
            headers,
            data
        });
    }

    public put(url: string, data: any = {}, headers: { [key: string]: string } = {}): Promise<any> {
        return this.request({
            url,
            method: 'PUT',
            headers,
            data
        });
    }

    public delete(url: string, params: any = {}, headers: { [key: string]: string } = {}): Promise<any> {
        return this.request({
            url,
            method: 'DELETE',
            headers,
            data: params
        });
    }
}


/*

const requestService = new RequestService('https://api.example.com');

requestService.get('/data')
  .then((res) => {
        console.log('GET 请求成功:', res);
    })
  .catch((err) => {
        console.error('GET 请求失败:', err);
    });

const postData = {
    username: 'exampleUser',
    password: 'examplePassword'
};
requestService.post('/login', postData)
  .then((res) => {
        console.log('POST 请求成功:', res);
    })
  .catch((err) => {
        console.error('POST 请求失败:', err);
    });

const putData = {
    id: 1,
    name: 'updatedName'
};
requestService.put('/items/1', putData)
  .then((res) => {
        console.log('PUT 请求成功:', res);
    })
  .catch((err) => {
        console.error('PUT 请求失败:', err);
    });

requestService.delete('/items/1')
  .then((res) => {
        console.log('DELETE 请求成功:', res);
    })
  .catch((err) => {
        console.error('DELETE 请求失败:', err);
    });
*/
